import 'package:flutter/material.dart';
import 'package:gold/base/config.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/size_extension.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:provider/provider.dart';
import 'package:gold/app/user_provider.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/ui/my/settings/settings_viewmodel.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// @description:
/// @author tangzhentao
/// @created at 21:38 2024/11/25

class SettingsPage extends StatelessWidget {
  const SettingsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<SettingsViewmodel>(
      appBar: (_) => xfgAppbar(title: '设置'),
      model: SettingsViewmodel(),
      onInit: (data) => data.loadCache(),
      builder: (ctx, settings) => SingleChildScrollView(
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              _itemWidget('完善个人信息', rightText: '去设置', height: XfgSize().size78, border: Border(top: singleBorderSide),
                  onTap: () => XfgNav.pushNamed(Routes.settingsProfile)
              ),
              SizedBox(height: XfgSize().size10),
              _itemWidget('修改密码', rightText: '修改', onTap: () => XfgNav.pushNamed(Routes.settingsPassword, arguments: false)),
              _itemWidget('修改手机号', rightText: '修改', onTap: () => XfgNav.pushNamed(Routes.settingsPhone)),
              _itemWidget('清除缓存', rightText: ctx.select<SettingsViewmodel, int>((k) => k.cache).storage, onTap: settings.clearCache),
              _itemWidget('系统权限', onTap: () {}),
              _itemWidget('用户注册协议', onTap: () => XfgNav.pushNamed(Routes.rules, arguments: {'title': '平台服务协议', 'config': SystemConfig.serviceAgreement})),
              _itemWidget('隐私权协议', onTap: () {}),
              _itemWidget('个人信息收集清单', onTap: () {}),
              _itemWidget('第三方个人信息共享清单', onTap: () {}),
              _itemWidget('注销用户', onTap: () {}),
              _itemWidget('关于', onTap: () {}),
              /// 退出登录
              Container(
                height: XfgSize().size92 + XfgSize().bottomBarHeight,
                padding: EdgeInsets.only(top: XfgSize().size42),
                color: XfgColor.white,
                alignment: Alignment.topCenter,
                child: TextButton(
                  onPressed: context.read<UserProvider>().signOut,
                  style: xfgButtonStyle(
                      Size(XfgSize().size342, XfgSize().size50),
                      color: XfgColor.transparent,
                      side: BorderSide(color: XfgColor.theme, width: XfgSize().size0_5),
                      radius: XfgSize().size50
                  ),
                  child: XfgText('退出登录', color: XfgColor.text1, fontSize: XfgSize().text20),
                ),
              )
            ]
        ),
      ),
    );
  }

  /// 通用Widget
  /// [text] 文字
  /// [border] 描边
  /// [rightText] 右边文字
  /// [onTap] 点击
  Widget _itemWidget(String text, {double? height, BoxBorder? border, String? rightText,VoidCallback? onTap}) => Ink(
    height: (height?? XfgSize().size58) + XfgSize().size0_5,
    decoration: BoxDecoration(color: XfgColor.white, border: border?? Border(bottom: singleBorderSide)),
    child: InkWell(
      onTap: onTap,
      child: Row(
        children: [
          SizedBox(width: XfgSize().size16),
          Expanded(child:  XfgText(text, fontSize: XfgSize().text18, color: XfgColor.text1D)),
          XfgText(rightText?? '', fontSize: XfgSize().text16, color: XfgColor.text3),
          SizedBox(width: XfgSize().size4),
          Image.asset(Res.imagesArrowRight, width: XfgSize().size10, height: XfgSize().size16),
          SizedBox(width: XfgSize().size16)
        ],
      ),
    ),
  );
}
